package com.tanhua.dubbo.api;

import com.tanhua.model.mongo.Comment;
import com.tanhua.model.mongo.Movement;
import com.tanhua.model.vo.PageResult;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.FindAndModifyOptions;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;

import java.util.List;

/**
 * @program: tanhua
 * @description:
 * @author: MR.peng
 * @create: 2022-03-13 23:33
 **/

@DubboService
public class CommentApiImpl implements CommentApi {


    @Autowired
    private MongoTemplate mongoTemplate;
    //根据动态id和类型查询动态数据
    @Override
    public List<Comment> findComments(String movementId, Integer commentType, Integer page, Integer pageSize) {

        //构建Criteria
        Criteria criteria = Criteria.where("publishId").is(new ObjectId(movementId))
                .and("commentType").is(commentType);
        Query query = Query.query(criteria).with(Sort.by(Sort.Order.desc("created"))).skip((page-1)*pageSize).limit(pageSize);
        List<Comment> comments = mongoTemplate.find(query, Comment.class);
        return comments;
    }


    //保存互动数据,返回互动总数
    @Override
    public Integer save(Comment comment) {
        Integer commentType = comment.getCommentType();
        //a.保存互动数据
        ObjectId movementId= comment.getPublishId();
        //查询动态对象
        Movement movement = mongoTemplate.findById(movementId, Movement.class);
        //设置动态发布人id到comment
        comment.setPublishUserId(movement.getUserId());

        //保存
        mongoTemplate.save(comment);
        //b.更新动态，获取最新的数据
        Criteria criteria = Criteria.where("id").is(movementId);
        Query query = Query.query(criteria);
        //更新字段
        Update update = new Update();
        if (commentType == 1){
            update.inc("likeCount",1);
        }else if(commentType == 2 ){
            update.inc("commentCount",1);
        }else {
            update.inc("loveCount",1);
        }
        //更新配置
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);//当更新成功后，获取最新的数据对象
        Movement andModify = mongoTemplate.findAndModify(query, update, options, Movement.class);//查询条件，更新的字段内容，配置，实体类

        if (commentType == 1){
            return andModify.getLikeCount();
        }else if(commentType == 2 ){
            return andModify.getCommentCount();
        }else {
            return andModify.getLoveCount();
        }
    }


    //删除互动数据,返回互动总数
    @Override
    public Integer delete(Long userId, Integer commentType, String id) {
        //1.删除互动数据  delete from  tab where userId = ? and commentType = ? and movementId = ?
        Criteria criteria = Criteria.where("userId").is(userId).and("commentType").is(commentType)
                .and("publishId").is(new ObjectId(id));
        Query query = Query.query(criteria);
        mongoTemplate.remove(query,Comment.class);


        //2.更新动态表总数字段，获取最新值
        //i.条件
        Criteria movementCriteria = Criteria.where("id").is(new ObjectId(id));
        Query movementQuery = Query.query(movementCriteria);
        //ii、更新的字段
        Update update = new Update();
        if (commentType == 1){
            update.inc("likeCount",-1);
        }else if(commentType == 2 ){
            update.inc("commentCount",-1);
        }else {
            update.inc("loveCount",-1);
        }
        //iii、更新配置
        FindAndModifyOptions options = new FindAndModifyOptions();
        options.returnNew(true);
        //i4、更新获取最新值
        Movement andModify = mongoTemplate.findAndModify(movementQuery, update, options, Movement.class);//更新之后获取最新的数据(query,update,options,实体类)
        if (commentType == 1){
            return andModify.getLikeCount();
        }else if(commentType == 2 ){
            return andModify.getCommentCount();
        }else {
            return andModify.getLoveCount();
        }
    }


    // 查看动态评论列表 翻页 后台系统
    @Override
    public PageResult findByComment(String id, Integer page, Integer pagesize) {
        Criteria criteria = Criteria.where("publishId").is(new ObjectId(id)).and("commentType").is(2);
        Query query = Query.query(criteria)
                .with(Sort.by(Sort.Order.desc("created")))
                .skip((page -1 ) * pagesize)
                .limit(pagesize);
        long count = mongoTemplate.count(query, Comment.class);
        List<Comment> comments = mongoTemplate.find(query, Comment.class);
        return new PageResult(page,pagesize,count,comments);
    }


//    //保存互动数据
//    @Override
//    public Integer save(Comment comment) {
//        //a.保存互动数据
//        ObjectId movementId= comment.getPublishId();
//        //查询动态对象
//        Movement movement = mongoTemplate.findById(movementId, Movement.class);
//        //设置动态发布人id到comment
//        comment.setPublishUserId(movement.getUserId());
//        //保存
//        mongoTemplate.save(comment);
//        //b.更新动态中的评论总数
//        Criteria criteria = Criteria.where("id").is(movementId);
//        Query query = Query.query(criteria);
//        //更新字段
//        Update update = new Update();
//        update.inc("commentCount",1);
//        mongoTemplate.updateFirst(query,update,Movement.class);//查询条件， 更新的字段内容 ，实体类
//        //c.返回互动总数
//        Movement movement1 = mongoTemplate.findById(movementId, Movement.class);
//        return movement1.getCommentCount();
//    }
}
